package me.khrystal.rxnews.app;

import android.util.SparseArray;

import com.socks.library.KLog;

/**
 * @FileName: me.khrystal.rxnews.app.AppManager.java
 * @Fuction: 管理主页面几个导航Activity的排序，由于跳转导航的Activity时设置了此标志位FLAG_ACTIVITY_REORDER_TO_FRONT，
 * 跳转的Activity若在栈中有实例会顶到栈顶，NewsActivity作为整个app结束的页面，需要这里管理导航的Activity的排序，
 * 按返回键的时候不是结束导航Activity而是跳转上一个导航Activity
 *
 * Notice :该类的spareArray存储的只是ClassName 因为在修改主题样式（夜间日间时）会将除当前页面的其他页面finish掉
 * 所以在返回到上一个页面 实际上是打开一个新的Activity
 * @author: kHRYSTAL
 * @email: 723526676@qq.com
 * @date: 2016-03-03 11:25
 * @UpdateUser:
 * @UpdateDate:
 */
public class AppManager {

//    volatile watch this:http://sakyone.iteye.com/blog/668091
//    变量被修饰为volatile 作用是保证在不同线程中调用时不存在拷贝
//    若变量被修改 每个线程如果正在使用这个变量时也会被修改
//    实际上就是不存在拷贝 内存中只有一份这个变量
    private static volatile AppManager instance;

    /**
     * 记录Activity的名称，我的思路是Activity用RxBus实现退出，
     * 这里用String记录Activity栈的情况而已
     */
    private static SparseArray<String> mNavActivityOrder;

    private AppManager(){}

    /**
     * get single instance
     * @return
     */
    public static AppManager getAppManager(){
        if (instance==null){
            synchronized (AppManager.class){
                if (instance==null){
                    instance = new AppManager();
                }
            }
        }
        return instance;
    }


    /**
     * 获取当前导航中Activity的上一个Activity
     * @return
     * @throws ClassNotFoundException
     */
    public Class getLastNavActivity() throws ClassNotFoundException{
        if (mNavActivityOrder == null
                ||mNavActivityOrder.size() == 0
                ||mNavActivityOrder.size() == 1) {
            return null;
        }
        return Class.forName(mNavActivityOrder.get(mNavActivityOrder.size()-2));
    }

    /**
     * 获取当前显示的Activity
     * @return
     * @throws ClassNotFoundException
     */
    public Class getCurrentNavActivity() throws ClassNotFoundException{
        if (mNavActivityOrder==null||mNavActivityOrder.size()==0){
            return null;
        }
        return Class.forName(mNavActivityOrder.get(mNavActivityOrder.size()-1));
    }

    public void orderNavActivity(String className,boolean backPress){
        if (mNavActivityOrder == null){
            mNavActivityOrder = new SparseArray<>();
        }
        //获取类的索引
        final int index = mNavActivityOrder.indexOfValue(className);
        //不是返回键 即点击跳转情况
        if (!backPress){
            if (index==-1){
                //还没有数据的话，保存当前classname至sparsearray末尾
                mNavActivityOrder.put(mNavActivityOrder.size(),className);
            }else {
                //如果有数据，将当前class置为末尾
                for (int i = index + 1;i<mNavActivityOrder.size();i++){
                    //将该index之后的className前移一位
                    mNavActivityOrder.put(i-1,mNavActivityOrder.valueAt(i));
                }
                mNavActivityOrder.put(mNavActivityOrder.size()-1,className);
            }
        }else {
            //返回键的情况，将位置最高的类名放到最低位
            className = mNavActivityOrder.valueAt(mNavActivityOrder.size()-1);
            for (int i = index;i>=0;i--){
                mNavActivityOrder.put(i+1,mNavActivityOrder.valueAt(i));
            }
            mNavActivityOrder.put(0,className);
        }

        printOrder();
    }

    private void printOrder() {
        KLog.e("PRINT:"+mNavActivityOrder.size());
        for (int i = 0; i < mNavActivityOrder.size(); i++) {
            KLog.e("PRINT:"+mNavActivityOrder.get(i));
        }
        KLog.e("PRINT END");
    }
}
